package com.chinawlx.wlxteacher.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.ui.activity.WLXStudentDetailActivity;
import com.chinawlx.wlxteacher.utils.WLXGreenDaoUtil;
import com.chinawlx.wlxteacher.utils.WLXTeacherConstant;
import com.chinawlx.wlxteacher.wlx_student;
import com.chinawlx.wlxteacher.wlx_studentDao;
import com.chinawlx.wlxteacher.wlx_student_order_detail;
import com.chinawlx.wlxteacher.wlx_student_order_detailDao;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 学生信息界面
 * <p/>
 * Created by yyc on 2016/8/2.
 */
public class WLXStudentInfoFragment extends WLXBaseFragment implements RadioGroup
        .OnCheckedChangeListener {
    @BindView(R.id.lv_student_info)
    ListView studentInfo;
    @BindView(R.id.rg_content_student_info)
    RadioGroup mRadioGroup;
    @BindView(R.id.rb_default1)
    RadioButton mDefault;
    @BindView(R.id.rb_lesson)
    RadioButton mLesson;
    @BindView(R.id.tv_sum)
    TextView sum;
    private ArrayList<StudentInfoBean> wlxStudents = new ArrayList<>();
    private List<Integer> selectList = Arrays.asList(R.id.rb_default1, R.id.rb_lesson);
    private WLXLoadingFragment wlxLoadingFragment;

    @Override
    protected View createView(LayoutInflater inflater, ViewGroup container, Bundle
            savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_student_info, null);
        ButterKnife.bind(this, view);
//        wlxLoadingFragment = WLXLoadingFragment.getInstance();
//        wlxLoadingFragment.show(getActivity().getSupportFragmentManager(), "");
        init();
        return view;
    }

    private void init() {
        initData();
        initView();
    }

    private void initData() {
        String gradeId = getArguments().getString(WLXTeacherConstant.GRADE_ID);
        List<wlx_student_order_detail> wlxStudentOrderDetails = WLXGreenDaoUtil
                .getStudentOrderDetailDao()
                .queryBuilder().where
                        (wlx_student_order_detailDao.Properties.Grade_id.eq(gradeId)).list();
        for (wlx_student_order_detail detail : wlxStudentOrderDetails) {
            StudentInfoBean bean = new StudentInfoBean();
            bean.setNum(detail.getBalance_number());
            bean.setStudent_id(detail.getStudent_id());
            List<wlx_student> students = WLXGreenDaoUtil.getStudentDao().queryBuilder().where
                    (wlx_studentDao.Properties.Student_id.eq(detail.getStudent_id())).list();
            wlx_student student = students.get(0);
            bean.setName(student.getRealname() + "");
            bean.setIcon(student.getAvatar_url() + "");

            wlxStudents.add(bean);
        }

//        for (wlx_student wlxStudent : wlxStudents) {
//            wlxStudentOrderDetails = WLXGreenDaoUtil.getStudentOrderDetailDao()
//                    .queryBuilder().where(wlx_student_order_detailDao.Properties.Student_id.eq
//                            (wlxStudent.getStudent_id())).list();
//        }
    }

    private void initView() {
        sum.setText("共" + wlxStudents.size() + "人");
        mRadioGroup.setOnCheckedChangeListener(this);
        mRadioGroup.check(R.id.rb_default1);
        studentInfo.setAdapter(new StudentInfoAdapter());
        studentInfo.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String studentId = Integer.toString(wlxStudents.get(position).getStudent_id());
                String gradeId = getArguments().getString(WLXTeacherConstant.GRADE_ID);
                Bundle bundle = new Bundle();
                bundle.putString(WLXTeacherConstant.GRADE_ID, gradeId);
                bundle.putString(WLXTeacherConstant.STUDENT_ID, studentId);
                startActivity(WLXStudentDetailActivity.class, false, bundle);
            }
        });
    }

    class StudentInfoAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return wlxStudents == null ? 0 : wlxStudents.size();
        }

        @Override
        public Object getItem(int position) {
            return wlxStudents.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            StudentInfoViewHolder holder = null;
            if (convertView == null) {
                holder = new StudentInfoViewHolder();
                convertView = View.inflate(getContext(), R.layout.item_stuent_info, null);
                holder.icon = (ImageView) convertView.findViewById(R.id.iv_icon_student_info);
                holder.name = (TextView) convertView.findViewById(R.id.tv_name_student_info);
                holder.num = (TextView) convertView.findViewById(R.id.tv_num_student_info);
                convertView.setTag(holder);
            } else {
                holder = (StudentInfoViewHolder) convertView.getTag();
            }
            String url = wlxStudents.get(position).getIcon();
            Glide.with(getContext()).load(url).into(holder.icon);
            holder.name.setText(wlxStudents.get(position).getName());
            holder.num.setText(wlxStudents.get(position).getNum()+"课次");
            return convertView;
        }
    }

    private static class StudentInfoViewHolder {
        ImageView icon;
        TextView name;
        TextView num;
    }
    //学生信息bean,保存查询到的学生信息
    class StudentInfoBean {
        int student_id;
        String icon;
        String name;
        int num;

        public int getStudent_id() {
            return student_id;
        }

        public void setStudent_id(int student_id) {
            this.student_id = student_id;
        }

        public String getIcon() {
            return icon;
        }

        public void setIcon(String icon) {
            this.icon = icon;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getNum() {
            return num;
        }

        public void setNum(int num) {
            this.num = num;
        }
    }
    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        int position = selectList.indexOf(checkedId);

    }
}
